package com.ecric.http.socket;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ecric.http.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_multi_thread_client)
public class MultiThreadClientActivity extends AppCompatActivity {
    // 输入信息框
    @ViewById(R.id.ed_multi_input)
    EditText edInput;
    //展示信息
    @ViewById(R.id.txt_show)
    TextView txtShow;
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //如果消息来自于子线程
            if (msg.what == 0x123){
                txtShow.append("\n" + msg.obj.toString());
            }
        }
    };
    //客户端处理数据的线程
    private ClientThread clientThread;
    @AfterViews
    public void initData(){
        clientThread = new ClientThread(handler);
        new Thread(clientThread).start();
//        ClientBody clientBody = new ClientBody("192.168.11.139",30010,"1");
//        clientBody.start();
    }
    /**
     * 发送监听
     * @param view
     */
    @Click(R.id.btn_send)
    public void onClick(View view){
        //当用户按下发送按钮后，将用户输入的数据，封装成Message
        //然后发送给子线程的handle对象
        Message message  = new Message();
        message.what = 0x345;
        message.obj = edInput.getText().toString();
        clientThread.revHandler.sendMessage(message);
        edInput.setText("");
    }
}
